package com.op.util;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;
import android.view.View;

import java.io.ByteArrayOutputStream;

import com.op.studyingliteapp.sender.Runtime;

/**
 * Created by Genji on 2015/4/11.
 */
public class ScreenCapUtil {
    static String TAG = "ScreenCapUtil";

    public static Bitmap init(View view) {

        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        view.buildDrawingCache();
        return view.getDrawingCache();


    }
//     public static Bitmap screenCapToBitmap(View view) {
//
//        return
//
//
//    }

    public static byte[] screenCapToByte(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        byte[] bytes;
        Log.i(TAG, "开始获取view cache");
//        Bitmap bitmap = screenCapToBitmap(view);

        if (bitmap.getWidth() * bitmap.getHeight() > 1280 * 800) {
            Log.i(TAG, "压缩bitmap");
            int t = bitmap.getWidth() * bitmap.getHeight() / 1280 / 800;
            t--;

            bitmap = decode(bitmap, bitmap.getWidth() / t, bitmap.getHeight() / t);
        }
        Log.i(TAG, "bitmap.getByteCount() " + bitmap.getWidth() + " " + bitmap.getHeight());
//        Bitmap bitmap = small(screenCapToBitmap(view));
        Log.i(TAG, "获取view cache完成");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Log.i(TAG, "Bitmap to ByteArrayOutputStream start");
        bitmap.compress(Bitmap.CompressFormat.WEBP, 70, baos);

        Log.i(TAG, "Bitmap to ByteArrayOutputStream end");
        Log.i(TAG, "ByteArrayOutputStream to byte[] start");
        bytes = baos.toByteArray();
        Log.i(TAG, "ByteArrayOutputStream to byte[] end");

        return bytes;


    }

    public static byte[] bitmapToByte(Bitmap bitmap) {
        byte[] bytes;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Log.i(TAG, "bitmapToByte start");
        bitmap.compress(Bitmap.CompressFormat.WEBP, 50, baos);
        Log.i(TAG, "bitmapToByte end");
        bytes = baos.toByteArray();
        return bytes;


    }

    private static Bitmap decode(Bitmap bitmap, int newWidth, int newHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    private static Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.5f, 0.5f); //长和宽放大缩小的比例
        Bitmap resizeBmp;
        if (bitmap.isRecycled()) {
            Log.i(TAG, "bitmap has been recycled");
            bitmap = init(Runtime.getView());
        }
        resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);


        return resizeBmp;
    }

}
